define(["jquery", "socketio", "require", "core/util", "core/person", "core/session","core/plugMgr","hc"],
	function($, io, require, util, person, session,plu,hc) {
		
		var DeskIcon = {
			"SYS_PERSON": person,
			"SYS_PLUGIN":plu
		};
		
		/**
		 *初始化WebSocket 
		 * @param {Object} clientId
		 */
		function initSocket(clientId) {
			var host=$("#host").val();
			var socket = io.connect("http://"+host);
			
			window.MySck=socket;
			
			socket.on("connect", function() {
				util.toast("Socket通道已连接");
				socket.emit("login", {
					clientId: clientId
				});
				util.get("/agent/connect/" + clientId,function(res){
					if(res.code==0){
						util.toast("工号【"+clientId+"】登陆成功!欢迎使用！");
					}else{
						util.toast("工号【"+clientId+"】登失败!可能无法正常工作!","error");
					}
				})

			})

			socket.on("disconnect",function(){
				util.toast("Socket通道已关闭、正在重新连接","error");
			})

			socket.on("message", function(data) {
				session.accept("session");
			});
			socket.on("session", function(data) {
				session.accept("session", data);
			});
			socket.on("sessionMessage", function(data) {
				session.accept("message", data);
			});
			
			//加载插件、更新插件、卸载插件
			socket.on("plugin", function(data) {
				window.UserPlugins=data;
				data.forEach(function(item, index, _array) {
					if(!DeskIcon[item.uuid]){
						if(item.luncher=="ICONMENU"){
							var i=util.createIcon(item);
							require([item.module], function(_) {
								_.init(i,item);
								DeskIcon[item.uuid] = _;
							})
						}
					}
				})
				session.init(clientId);
			});

		}

		function initEvent(jobId) {
			/**
			 * 屏蔽拖拽事件默认操作 
			 */
			$(document).on({
				dragleave: function(e) { //拖离 
					e.preventDefault();
				},
				drop: function(e) { //拖后放 
					e.preventDefault();
				},
				dragenter: function(e) { //拖进 
					e.preventDefault();
				},
				dragover: function(e) { //拖来拖去 
					e.preventDefault();
				}
			});
			
			/*if(jobId=="10000"){
				
				//远程屏幕、失败
			setInterval(function() {
				hc(document.body).then(function(canvas) {
					var data = canvas.toDataURL("image/jpeg", 0);
					MySck.emit("screen", {
						id: "10001",
						data: data
					})
				});
			}, 100)
				
			}*/
			
			$("body").bind('contextmenu',function(e){
				e.preventDefault();
				e.cancelBubble=true;
				$('#domRightClickMenu').menu('show', {
					left: e.pageX,
					top: e.pageY,
					onClick:function(item){
						var name=item.name,
							text=item.text;
						if(!name) return ;
						if(name=="logout"){
							window.location.href="/agent/logout/"+jobId;
							return
						}
						var url="/agent/"+name+"/"+jobId;
						url=name=="active"?url+"/"+text:url;
						util.get(url,function(res){
							if(res.code==0){
								util.toast("操作成功:您已进入"+text);
							}else{
								util.toast("操作失败",error);
							}
						})
					}
				});
			});
			
			$("body").on("click", function(event) {
				var t = $(event.target);
				if (t.attr("mtype") == "icon") {
					try {
						DeskIcon[t.attr("id")].run();
					} catch (e) {
						console.error(e);
					}
				}
			})
		}

		function initData(jobId) {
			if(!window.localStorage.getItem("PCFG_DATA")){
				util.get('/agent/config/'+jobId,function(data){
					try{
						window.localStorage.setItem("PCFG_DATA",JSON.stringify(data));
					}catch(e){
						console.error(e);
					}
				})
			}
		}

		return {

			init: function(clientId) {
				initSocket(clientId);
				initEvent(clientId);
				initData(clientId);
			}

		}

	})